//
//  AttentionListViewController.m
//  YoucApp
//
//  Created by Liam on 11/25/12.
//
//

#import "AttentionListViewController.h"
#import "UserManager.h"
#import "LoadMoreItem.h"
#import "YAObjGame.h"
#import "AttentionManager.h"

@interface AttentionListViewController ()

@end

@implementation AttentionListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoginDidFinished:) name:UMUserLoginDidFinishedNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	
	//[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - override method

- (NSString *)getCellIdentifierByItem:(id)item
{
	if ([item isKindOfClass:[YAObjGame class]])
    {
        return @"AttentionGameCell";
    }
    else
    {
        return [super getCellIdentifierByItem:item];
    }
}

- (NSString *)gameListNotificationName
{
    return LMGetAttentionListDidFinishedNotification;
}

- (BOOL)loadList:(NSInteger)start limit:(NSInteger)limit
{
	if ([[UserManager instance] checkUserLogin])
	{
		LMAttentionListParam *param = [[[LMAttentionListParam alloc] init] autorelease];
		param.context = self;
		
		[[ListManager instance] getAttentionList:param forceLoad:YES];
		
		return YES;
	}
	else
	{
		return NO;
	}
}

#pragma mark - local method

#pragma mark - UITableViewDataSource

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	return YES;
}

#pragma mark - UITableViewDelegate

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	id item = [self itemAtIndexPath:indexPath];
	
	if (!tableView.editing && [item isKindOfClass:[YAObjGame class]])
		return UITableViewCellEditingStyleDelete;
	else
		return UITableViewCellEditingStyleNone;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (editingStyle == UITableViewCellEditingStyleDelete)
	{
		// Delete the row from the data source.
		YAObjGame * item = [self itemAtIndexPath:indexPath];
		if ([item isKindOfClass:[YAObjGame class]])
		{
			[[AttentionManager instance] deleteAttention:item.gameId];
		}
		NSArray *indexs = [self deleteItemAtIndexPath:indexPath];
		[tableView deleteRowsAtIndexPaths:indexs withRowAnimation:UITableViewRowAnimationFade];
	}
}

@end
